﻿using Sidvall.Security;
using System.Threading.Tasks;

namespace Sidvall.AuthorizationManager.Security
{
    public class AuthorizationProviderFactory : Sidvall.Reflection.IInstanceItemFactory<IAuthorizationProvider>
    {
        #region Public Members

        #region CreateAsync

        public async Task<IAuthorizationProvider> CreateAsync(string context)
        {
            var factoryContext = AuthorizationProviderFactoryContext.Deserialize(context, new AuthorizationProviderFactoryContext());
            var item = new AuthorizationProvider();
            await item.LoadFromAuthorizationScopeIdAsync(factoryContext.AuthorizationScopeId, factoryContext.CachePolicy).ConfigureAwait(false);
            var userValidators = await Sidvall.SystemContext.Current.CodeManager.CreateInstancesAsync<IUserValidator>(factoryContext.UserValidators).ConfigureAwait(false);
            item.UserValidators.AddRange(userValidators);
            return item;
        }

        #endregion

        #endregion
    }
}
